package com.girl.girl.service;

import com.girl.girl.enums.ResultEnum;
import com.girl.girl.exception.ExceptionHandle;
import com.girl.girl.exception.GilrException;
import com.girl.girl.repository.GirlReposties;
import com.girl.girl.domain.Girl;
import com.sun.java.swing.plaf.windows.WindowsTreeUI;
import jdk.management.resource.internal.inst.FileOutputStreamRMHooks;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

/**
 * Created by qxl on
 * 2018/7/3
 */
@Service
public class GirlService {

    @Autowired
    private GirlReposties girlReposties;

    @Transactional
    public void insertTwo(){
        Girl girlA = new Girl();
        girlA.setCupSize("A");
        girlA.setAge(18);
        this.girlReposties.save(girlA);
        Girl girlB = new Girl();
        girlB.setCupSize("CCCCC");
        girlB.setAge(19);
        this.girlReposties.save(girlB);
    }

    public void getAgea(Integer age){
        List<Girl> byAge = this.girlReposties.findByAge(age);
        if (CollectionUtils.isEmpty(byAge)){
            return;
        }
        for (Girl girl: byAge){
            Integer age1 = girl.getAge();
            if (age1 < 10){
                throw new GilrException(ResultEnum.PRIMART_SCHOOL);
            }else if (age >10  && age <18){
                throw new GilrException(ResultEnum.MIDDLE_SCHOOL);
            }
        }
    }

    public void  getAge(Integer age){
        List<Girl> byAge = this.girlReposties.findByAge(age);
        if (CollectionUtils.isEmpty(byAge)){
            return;
        }
        for (Girl girl: byAge){
            Integer age1 = girl.getAge();
            if (age1 < 10){
                throw new GilrException(ResultEnum.PRIMART_SCHOOL);
            }else if (age >10  && age <18){
                throw new GilrException(ResultEnum.MIDDLE_SCHOOL);
            }
        }
    }
}
